import express from "express";
import { Prisma, PrismaClient, test_table as PTestTable } from "@prisma/client";
import { Create, CreateId, Result, Time } from "@/utils";

const prisma = new PrismaClient();
const router = express.Router();

/** 测试 */
router.post("/test", async (req, res) => {
   const { total } = req.body;
   let value: any;

   let created;

   if (total) {
      created = await Create.IDs({ tableName: "test_table", total }, async ids => {
         const dataList: Prisma.test_tableCreateManyInput[] = [];

         ids.forEach(item => {
            dataList.push({
               id: item,
               created_time: Time.Now()
            });
         });

         value = await prisma.test_table.createMany({
            data: dataList
         });
      });
   } else {
      created = await Create.ID({ tableName: "test_table" }, async id => {
         value = await prisma.test_table.create({
            data: {
               id,
               created_time: Time.Now()
            }
         });
      });
   }

   if (created === null) return res.send(Result.Error());

   res.send(Result.OK(value));
});

export const test = router;
